﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NHibernate;
using ZLCMS.Business;
using Newtonsoft.Json;
using ZLCMS.Data.Model;
using DotNet.Common.NHibernateUtil;
using ZLCMS.Business.Util;

namespace ZLCMS.Web.SystemManage.Basics
{
    public partial class MenuConfigInterface : System.Web.UI.Page
    {
        public string _userAccount;

        protected void Page_Load(object sender, EventArgs e)
        {
            _userAccount = UserKit.GetUserAccountFailLogin();
            //判断操作类型
            string opt = Request["opt"];
            if (opt == "load_data")
            {
                LoadData();
            }
            else if (opt == "update")
            {
                Update();
            }
            else if (opt == "update_ex")
            {
                UpdateEx();
            }
            else if (opt == "add")
            {
                Add();
            }
            else if (opt == "remove")
            {
                Remove();
            }
        }

        #region 加载产品类型列表 + public void LoadData()
        /// <summary>
        /// 加载产品类型列表
        /// </summary>
        public void LoadData()
        {
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                IList<SysMenuInfo> list = session.CreateQuery("from SysMenuInfo order by Menuorder").List<SysMenuInfo>();

                Response.Write(JsonConvert.SerializeObject(list));
                Response.End();
            }
        }
        #endregion

        #region 添加 + public void Add()
        /// <summary>
        /// 添加
        /// </summary>
        public void Add()
        {
            int intData;

            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                SysMenuInfo item = new SysMenuInfo();

                item.Title = Request["Title"];
                item.Menuurl = "#";
                item.Hotkey = "";
                item.Menuorder = 99;
                item.Menulevel = 0;

                if (Int32.TryParse(Request["Preid"], out intData))
                {
                    item.Preid = intData;
                }
                else
                {
                    item.Preid = 0;
                }
                item.Param1 = "ADMIN";

                session.Save(item);
                session.Flush();

                Response.Write(JsonConvert.SerializeObject(item));
                Response.End();
            }
        }
        #endregion

        #region 更新 + public void Update()
        /// <summary>
        /// 更新
        /// </summary>
        public void Update()
        {
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                SysMenuInfo item = session.Get<SysMenuInfo>(Int32.Parse(Request["Id"]));
                item.Title = Request["Title"];
                session.Update(item);
                session.Flush();

                Response.Write(JsonConvert.SerializeObject(item));
                Response.End();
            }
        }
        #endregion

        #region 更新类别详细设置 + public void UpdateEx()
        /// <summary>
        /// 更新类别详细设置
        /// </summary>
        public void UpdateEx()
        {
            int intData;

            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                SysMenuInfo item = session.Get<SysMenuInfo>(Int32.Parse(Request["Id"]));

                if (Int32.TryParse(Request["Preid"], out intData))
                {
                    item.Preid = intData;
                }

                if (Int32.TryParse(Request["Sort"], out intData))
                {
                    item.Menuorder = intData;
                }

                item.Title = Request["Title"];
                item.Menuurl = Request["Url"];
                item.Param1 = Request["Param1"];
                item.Param2 = Request["Param2"];
                item.Param3 = Request["Param3"];

                session.Update(item);
                session.Flush();

                Response.Write(JsonConvert.SerializeObject(item));
                Response.End();
            }
        }
        #endregion

        #region 获取子节点 + public void GetChildNodeIds(IList<ShopCategory> list, int id, ref List<int> ids)
        /// <summary>
        /// 获取子节点ID
        /// </summary>
        /// <param name="list"></param>
        /// <param name="id"></param>
        /// <param name="ids"></param>
        public void GetChildNodeIds(IList<SysMenuInfo> list, int id, ref List<int> ids)
        {
            foreach (SysMenuInfo type in list)
            {
                if (type.Id == id)
                {
                    ids.Add(type.Id);
                }
                if (type.Preid == id)
                {
                    GetChildNodeIds(list, type.Id, ref ids);
                }
            }
        }
        #endregion

        #region 删除 + protected void Remove()
        /// <summary>
        /// 删除
        /// </summary>
        protected void Remove()
        {
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                int nodeId = Int32.Parse(Request["Id"]);

                IList<SysMenuInfo> list = session.CreateQuery("from SysMenuInfo").List<SysMenuInfo>();
                List<int> ids = new List<int>();

                GetChildNodeIds(list, nodeId, ref ids);

                session.CreateQuery("delete from SysMenuInfo where Id in (:ids)")
                    .SetParameterList("ids", ids.ToArray())
                    .ExecuteUpdate();

                session.Flush();
                Response.Write("ok");
                Response.End();
            }
        }
        #endregion

    }
}